package com.yum.web.controller.sys;

import com.yum.common.domain.R;
import com.yum.common.utils.Utils;
import com.yum.system.entity.SysUser;
import com.yum.system.service.SysUserService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Package: com.yum.web.controller
 * Description： TODO
 * Author: Forest
 * Date: Created in 2021/3/28 14:56
 * Version: 0.0.1
 * Modified By: duforest
 */
@Api(tags = "系统用户")
@RestController
@RequestMapping("sys/user")
public class SysUserController {

    private static final Logger log = LoggerFactory.getLogger(SysUserController.class);


    @Resource
    private SysUserService sysUserService;


    @ApiOperation("新增用户")
    @ApiImplicitParam(name = "sysUser", value = "新增用户信息", dataType = "SysUser")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public R addUser(@RequestBody SysUser sysUser) {
        if (Utils.isNull(sysUser) || Utils.isNull(sysUser.getUserCode())) {
            return R.error("用户UserCode不能为空");
        }
        int rows = sysUserService.insertUser(sysUser);
        return rows > 0 ? R.success() : R.error();
    }

    @ApiOperation("更新用户")
    @ApiImplicitParam(name = "sysUser", value = "新增用户信息", dataType = "SysUser")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public R updateUser(@RequestBody SysUser sysUser) {
        int rows = sysUserService.updateUser(sysUser);
        return rows > 0 ? R.success() : R.error();
    }


    @ApiResponse(code = 200, message = "success")
    @ApiOperation("删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Long", required = true)})
    @GetMapping("/delete/{userId}")
    @ResponseBody
    public R deleteUserById(@PathVariable("userId") Long userId) {
        int rows = sysUserService.deleteUserById(userId);
        return rows > 0 ? R.success() : R.error();
    }

    @ApiResponse(code = 200, message = "success")
    @ApiOperation("根据ID查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Long", required = true)})
    @GetMapping("/qry/{userId}")
    @ResponseBody
    public SysUser selectUserById(@PathVariable("userId") Long userId) {
        return sysUserService.selectUserById(userId);
    }

    @ApiOperation("查询用户")
    @ApiImplicitParam(name = "sysUser", value = "查询用户", dataType = "SysUser")
    @RequestMapping(value = "/selectUsers", method = RequestMethod.POST)
    @ResponseBody
    public List<SysUser> selectUsers(@RequestBody SysUser sysUser) {
        return sysUserService.selectUserList(sysUser);
    }



}
